package cn.dt70.controller;


import cn.dt70.pojo.AqbCreditor;
import cn.dt70.pojo.AqbInvestment;
import cn.dt70.pojo.AqbProduct;
import cn.dt70.pojo.AqbProject;
import cn.dt70.service.AqbCreditorService;
import cn.dt70.service.AqbInvestmentService;
import cn.dt70.service.AqbProductService;
import cn.dt70.service.AqbProjectService;
import cn.dt70.utils.CompareDate;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/lzy/")
public class ControllerLzy {


    @Resource
    private AqbInvestmentService aqbInvestmentService;

    @Resource
    private AqbCreditorService aqbCreditorService;

    @Resource
    private AqbProjectService aqbProjectService;

    @Resource
    private AqbProductService aqbProductService;

    //查看投资记录
    @RequestMapping("investment")
    public String displayInvestment(@RequestParam("id") String id,@RequestParam(value = "opr",required = false)String opr,@RequestParam(value = "begin",required = false)String begin,@RequestParam(value = "end",required = false)String end, Model model) throws Exception {
        if(null!=opr) {
            //一周内的投资
            if (opr.equals("week")) {
                List<AqbInvestment> InvestmentList = aqbInvestmentService.findByIdInWeek(Integer.parseInt(id));
                model.addAttribute("InvestmentList", InvestmentList);
                return "usercenter-investcontrol-investrecord";
                //一月内的投资
            } else if (opr.equals("month")) {
                List<AqbInvestment> InvestmentList = aqbInvestmentService.findByIdInMonth(Integer.parseInt(id));
                model.addAttribute("InvestmentList", InvestmentList);
                return "usercenter-investcontrol-investrecord";
                //三月内的投资
            } else if (opr.equals("threeMonth")) {
                List<AqbInvestment> InvestmentList = aqbInvestmentService.findByIdInThreeMonth(Integer.parseInt(id));
                model.addAttribute("InvestmentList", InvestmentList);
                return "usercenter-investcontrol-investrecord";
            }
        }

        //一段时间内的投资
        if(begin!=null&&end!=null){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");

            List<AqbInvestment> InvestmentList = aqbInvestmentService.findByIdForTime(Integer.parseInt(id), simpleDateFormat.parse(begin),simpleDateFormat.parse(end));
            model.addAttribute("InvestmentList",InvestmentList);
            return "usercenter-investcontrol-investrecord";
        }

        List<AqbInvestment> InvestmentList = aqbInvestmentService.findById(Integer.parseInt(id));
        model.addAttribute("InvestmentList",InvestmentList);
            return "usercenter-investcontrol-investrecord";
    }

    @RequestMapping("transfer")
    public String displayTransfer(@RequestParam("id") String id,Model model) throws Exception {
        List<AqbInvestment> InvestmentList = aqbInvestmentService.findCreditorById(Integer.parseInt(id));
        model.addAttribute("InvestmentList",InvestmentList);
          return "usercenter-investcontrol-transfer";
    }


    //添加投资记录
    @RequestMapping("addInvestment")
    public String addInvestment(@RequestParam("pid") String pid,@RequestParam("amount") String amount,@RequestParam("uid") String uid,Model model) throws Exception {
        AqbProject aqbProject = aqbProjectService.findOneProject(Integer.parseInt(pid),null);
        BigDecimal rate = aqbProject.getRate();
        Integer status= aqbProject.getProStatus();
        Date fullDate = aqbProject.getFullDate();
        Integer period = aqbProject.getPeriod();
        Date date = new Date();
        long Day = (date.getTime()-fullDate.getTime())/86400000;
        Integer day = (int)Day;
        Integer restDay = period*30-day;
        AqbInvestment aqbInvestment = new AqbInvestment();
        aqbInvestment.setProgramId(Integer.parseInt(pid));
        aqbInvestment.setInvestorId(Integer.parseInt(uid));
        aqbInvestment.setInvestAmount(Integer.parseInt(amount));
        aqbInvestment.setRestAmount(Integer.parseInt(amount));
        aqbInvestment.setRate(rate);
        aqbInvestment.setRestDay(restDay);
        aqbInvestment.setInvestDate(new Date());
        aqbInvestment.setStaus(status);
        aqbInvestmentService.addInvestment(aqbInvestment);
        List<AqbInvestment> InvestmentList = aqbInvestmentService.findById(Integer.parseInt(uid));
        model.addAttribute("InvestmentList",InvestmentList);
        return "usercenter-investcontrol-investrecord";
    }


    //转让债权
    @RequestMapping("TrsnsferInvestment")
    public String TrsnsferInvestment(@RequestParam("investId") String investId,Model model) throws Exception {
        AqbInvestment aqbInvestment= aqbInvestmentService.findByInvestId(Integer.parseInt(investId));
        AqbProject aqbProject = aqbProjectService.findOneProject(aqbInvestment.getProgramId(),null);
        AqbProduct aqbProduct = aqbProductService.findOneProjectDetail(aqbInvestment.getProgramId(),null);
        model.addAttribute("aqbInvestment",aqbInvestment);
        model.addAttribute("aqbProject",aqbProject);
        model.addAttribute("aqbProduct",aqbProduct);
        return "transferForm";
    }


    //添加债权转让记录
    @RequestMapping("addCreditor")
    public String addCreditor(@RequestParam("pid") String pid,@RequestParam("investor") String investor,@RequestParam("invest_money") String invest_money,@RequestParam("transfer_money") String transfer_money,@RequestParam("rate") String rate) throws Exception {
        AqbCreditor aqbCreditor = new AqbCreditor();
        int transferRate = Integer.parseInt(invest_money)/Integer.parseInt(transfer_money);
        aqbCreditor.setpId(Integer.parseInt(pid));
        aqbCreditor.setInvestorId(Integer.parseInt(investor));
        aqbCreditor.setSubscribeMoney(Integer.parseInt(invest_money));
        aqbCreditor.setTransferMonry(Integer.parseInt(transfer_money));
        aqbCreditor.setTransferTime(java.sql.Date.valueOf(CompareDate.getResult(0)));
        aqbCreditor.setCurrentMoney(0);
        aqbCreditor.setTransferStatus(0);
        aqbCreditor.setCurrentRate(new BigDecimal(rate));
        aqbCreditor.setTransferRate((double)transferRate);
        aqbCreditorService.addCreditor(aqbCreditor);
        return "usercenter-investcontrol-investrecord";
    }


    //查看正在转让的债权
    @RequestMapping(value = "transfering",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String displayTransfering(@RequestParam("id") String id,Model model) throws Exception {
        List<AqbCreditor>  CreditorList = aqbCreditorService.findCreditorById(Integer.parseInt(id));
        StringBuffer sb = new StringBuffer("{");
        sb.append("\"CreditorList\":");
        String CreList = JSON.toJSONString(CreditorList);
        sb.append(CreList);
        sb.append("}");
        return sb.toString();
    }

     //查看可转让的债权
    @RequestMapping(value = "transferCan",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String displayTransferCan(@RequestParam("id") String id,Model model) throws Exception{
        List<AqbInvestment> InvestmentList = aqbInvestmentService.findCreditorById(Integer.parseInt(id));
        StringBuffer sb = new StringBuffer("{");
        sb.append("\"InvestmentList\":");
        String CreList = JSON.toJSONString(InvestmentList);
        sb.append(CreList);
        sb.append("}");
        return sb.toString();
    }


}


